package com.rtx.wms.std.handler.cancle;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.wms.std.asn.bean.Receipt;
import com.rtx.wms.std.asn.mapper.ReceiptMapper;
import com.rtx.wms.std.so.bean.ApiCancle;
import com.rtx.wms.std.so.bean.Orders;
import com.rtx.wms.std.so.mapper.ApiCancleMapper;
import com.rtx.wms.std.so.mapper.OrdersMapper;

import cn.hutool.core.util.StrUtil;

@Service
public class CancleService {
	
	Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	ApiCancleMapper apiCancleMapper;
	
	@Autowired
	OrdersMapper ordersMapper;
	
	@Autowired
	ReceiptMapper receiptMapper;
	
	@DataSourceAnnotation
	public void handel(String datasoucekey) {
		List<ApiCancle> list = queryCancle();
		for(ApiCancle cancle:list) {
			String msg = "";
			if("so".equals(cancle.getType())) {
				msg = handelso(cancle);
			}else {
				
				msg = handelasn(cancle);
			}
			
			updStatus(msg,cancle);
		}
	}
	
	private List<ApiCancle> queryCancle(){
		LambdaQueryWrapper<ApiCancle> query = Wrappers.lambdaQuery();
		query.in(ApiCancle::getEdiflag, "P","PS");
		query.lt(ApiCancle::getRtxqueu, 20);
		return apiCancleMapper.selectList(query);
	}
	
	private String handelso(ApiCancle apiCancle) {
		String msg = "ok";
		LambdaQueryWrapper<Orders> query = Wrappers.lambdaQuery();
		query.eq(Orders::getExternorderkey, apiCancle.getExternkey());
		List<Orders> list = ordersMapper.selectList(query);
		for(Orders o:list) {
			try {
				int status = Integer.parseInt(o.getStatus());
				if(status > 67) {
					throw new RuntimeException("单据状态大于67不能取消");
				}
				LambdaUpdateWrapper<Orders> upd = Wrappers.lambdaUpdate();
				upd.set(Orders::getStatus, "98");
				upd.set(Orders::getRtxcancel, "98");
				upd.eq(Orders::getExternorderkey, apiCancle.getExternkey());
				ordersMapper.update(null, upd);
			} catch (Exception e) {
				log.error("更新取消状态异常");
				msg = e.getMessage();
			}

		}
		return msg;
	}
	
	private String handelasn(ApiCancle apiCancle) {
		String msg = "ok";
		LambdaQueryWrapper<Receipt> query = Wrappers.lambdaQuery();
		query.eq(Receipt::getReceiptkey, apiCancle.getExternkey());
		List<Receipt> list = receiptMapper.selectList(query);
		for(Receipt o:list) {
			try {
				LambdaUpdateWrapper<Receipt> upd = Wrappers.lambdaUpdate();
				upd.set(Receipt::getStatus, "98");
				upd.eq(Receipt::getExternreceiptkey, apiCancle.getExternkey());
				receiptMapper.update(null, upd);
			} catch (Exception e) {
				log.error("更新取消状态异常");
				msg = e.getMessage();
			}

		}
		return msg;
	}
	
	private void updStatus(String msg,ApiCancle apiCancle) {
		String satus = "";
		if("ok".equals(msg)) {
			satus ="PS";
		}else {
			satus ="P";
		}
		
		LambdaUpdateWrapper<ApiCancle> upd = Wrappers.lambdaUpdate();
		upd.eq(ApiCancle::getExternkey, apiCancle.getExternkey());
		upd.set(ApiCancle::getEdiflag, satus);
		upd.set(ApiCancle::getMsg, StrUtil.sub(msg,0,400));
		upd.set(ApiCancle::getRtxqueu, apiCancle.getRtxqueu()+1);
		apiCancleMapper.update(null, upd);
	}
}
